%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% File: analyzeMotorTiming.m
% Co: The Neurosciences Institute
% Author: Richard G. Martin
% Purpose: Analyze Motor Data for movements during matching trials.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function trial_time_list_ms = analyzeMotorTiming(motor_data, phase_start_msec, phase_end_msec , trial_duration_msec, start_time_msec, end_time_msec)

%To Do: very specific function, so update script to incorporate
% phase_end_msec, start/end_time_msec

trial = 1;
pose_threshold = 0.25;%arm maximum difference from home pose to match pose
start_pose = 0;
for i=1:20:length(motor_data)
    %start analyzing when we have data
    if mod(motor_data(i,1), trial_duration_msec) == phase_start_msec
        motor_data(i,1)
        start_pose = motor_data(i,2) + motor_data(i,3);
        for j=1:40
            next_pose = motor_data(i+j-1,2) + motor_data(i+j-1,3);
            pose_diff = abs(next_pose-start_pose);
            if pose_diff > pose_threshold
               trial_time_list_ms(trial) = motor_data(i+j-1,1) - motor_data(i,1);
               break
            end
        end
      trial = trial + 1;   
     end
end

%reshape data for specific Shepard Plot
%**
% null_value = 2000; %this value is set according to analyzeTiming, ie non-match 
% null_block = null_value*ones(4,4);
% block_times = reshape(trial_time_list_ms,4,8)';
% mirJ_times = block_times(1:4,:);
% J_times = block_times(5:8,:);
% mirJ_times = [mirJ_times null_block];
% J_times = [null_block J_times];
% all_times = [mirJ_times; J_times];
% trial_time_list_ms = reshape(all_times',1,64);
%**
%return modified trial_time_list_ms modifed for plotShepard function